#include "singlemodelwidget.h"
#include "ui_singlemodelwidget.h"

#include "config.h"
#include "utils.h"
#include "path.h"

#include <QSettings>

static constexpr const char* SETTINGS_SINGLEMODE_PATH = "single_mode_path";

SingleModelWidget::SingleModelWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SingleModelWidget)
{
    ui->setupUi(this);

    QObject::connect(qApp, &QApplication::aboutToQuit, this, [this](){
        QString path = ui->lineEditPath->text();
        QSettings settings(SETTINGS_ORGANIZATION, PATHSEPARATOR_APPLICATION_NAME);
        settings.setValue(SETTINGS_SINGLEMODE_PATH, path);
    });

    connect(ui->btnPastePath, &QPushButton::clicked, this, [this]() {
        QString path = Utils::getClipboardText();
        ui->lineEditPath->clear();
        ui->lineEditPath->setText(path);
    });

    connect(ui->btnCopyPosix, &QPushButton::clicked, this, [this]() {
        QString path = ui->lineEditPosixPath->text();
        if (!path.isEmpty()) {
            Utils::setClipboardText(path);
        }
    });
    connect(ui->btnCopyWin, &QPushButton::clicked, this, [this]() {
        QString path = ui->lineEditWinPath->text();
        if (!path.isEmpty()) {
            Utils::setClipboardText(path);
        }
    });

    connect(ui->lineEditPath, &QLineEdit::textChanged, this, [this](const QString& text) {
        Path path(text);
        ui->lineEditPosixPath->setText(path.posixStyle());
        ui->lineEditWinPath->setText(path.windowsStyle());
    });

    QSettings settings(SETTINGS_ORGANIZATION, qApp->applicationName());
    ui->lineEditPath->setText(settings.value(SETTINGS_SINGLEMODE_PATH).toString());
}

SingleModelWidget::~SingleModelWidget()
{
    delete ui;
}
